package avicit.bdp.oss.service.auth;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.oss.dao.ObjectUserRelDao;
import avicit.bdp.oss.dto.ObjectUserRelDto;
import avicit.bdp.oss.utils.common.Constants;
import avicit.platform6.api.system.ConvertColumnClient;
import avicit.platform6.api.system.impl.SystemConstant;
import avicit.platform6.commons.utils.BusinessUtil;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.core.context.ThreadContextHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @金航数码科技有限责任公司 
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间：2021-01-12
 * @类说明：ObjectUserRelService
 * @修改记录：
 * @注意事项：
 * @功能：对象-用户关系
 */
@Service
@Transactional
public class ObjectUserRelService extends BaseService<ObjectUserRelDao, ObjectUserRelDto> {

  @Autowired ObjectUserRelDao objectUserRelDao;

  /** 获取系统用户名 */
  @Autowired private ConvertColumnClient convertColumnClient;

  /**
   * 查询对象-用户映射关系
   *
   * @param bucketName
   * @param prefix
   * @param objectName
   * @param userId
   * @return
   */
  public ObjectUserRelDto queryObjectUserRelDto(
      String bucketName, String prefix, String objectName, String userId) {
    return this.objectUserRelDao.queryObjectUserRelDto(bucketName, prefix, objectName, userId);
  }

  /**
   * 统计桶/对象-用户映射关系数量
   *
   * @param bucketName
   * @param prefix
   * @param objectName
   * @param userId
   * @return
   */
  public Integer countObjectUserRelDto(
      String bucketName, String prefix, String objectName, String userId) {
    return this.objectUserRelDao.countObjectUserRelDto(bucketName, prefix, objectName, userId);
  }

  /**
   * 删除对象-用户映射关系
   *
   * @param bucketName
   * @param prefix
   * @param objectName
   * @param userId
   */
  public void delObjectUserRelDto(
      String bucketName, String prefix, String objectName, String userId) {
    this.objectUserRelDao.delObjectUserRelDto(bucketName, prefix, objectName, userId);
  }

  /**
   * 对外接口，保存桶-用户映射关系；
   *
   * @param bucketId
   * @param bucketName
   */
  public void saveBucketUsrRel(String bucketId, String bucketName) {
    ObjectUserRelDto objectUserRelDto =
        ObjectUserRelDto.builder()
            .id(ComUtil.getId())
            .bucketName(bucketName)
            .bucketId(bucketId)
            .userId(getUserId())
            .build();

    this.insert(objectUserRelDto);
  }

  /**
   * 对外接口，删除桶-用户映射关系
   *
   * @param bucketId
   * @param bucketName
   */
  public void delBucketUsrRel(String bucketId, String bucketName) {
    this.objectUserRelDao.delBucketUsrRel(bucketId, bucketName, getUserId());
  }

  /**
   * 获取当前登录用户信息
   *
   * @return
   */
  private String getUserId() {
    return ThreadContextHelper.getUserId() == null
        ? Constants.DEFALUT_USER_ID
        : ThreadContextHelper.getUserId();
  }

  /**
   * 根据用户ID获取用户名
   *
   * @param userIds
   * @return
   * @throws Exception
   */
  public String getUserNameByIds(String userIds) {
    if (StringUtils.isEmpty(userIds)) {
      return null;
    }

    Map<String, Set<String>> convertFormData = new HashMap<>(16);
    String[] array = userIds.split(",");
    for (String userId : array) {
      BusinessUtil.createConvertSet(convertFormData, SystemConstant.USER, userId);
    }

    StringBuffer buf = new StringBuffer(Constants.BLACK);
    if (convertFormData.size() > 0) {
      // 获取请求结果
      Map<String, Map<String, String>> convertResultData =
          convertColumnClient.replace(convertFormData);
      for (String userId : array) {
        String name = BusinessUtil.convertFormat(convertResultData, SystemConstant.USER, userId);
        buf.append(name).append(Constants.COMMA);
      }
    }

    String names = buf.toString();
    if (names.length() >= 1) {
      names = names.substring(0, names.length() - 1);
    }
    return names;
  }
}
